// MyFormView.cpp : implementation file
//

#include "stdafx.h"
#include "TestAnaly.h"
#include "MyFormView.h"


// MyFormView

IMPLEMENT_DYNCREATE(MyFormView, CFormView)

MyFormView::MyFormView()
	: CFormView(MyFormView::IDD)
{
	m_bInit  = false;
	m_pRuler = NULL;

	m_fRealLenMMPerPt = 1.0f;
	m_fZoomFactor  = 1.0f;
	m_ImageSize.cx = 0;
	m_ImageSize.cy = 0;

	InitView();
}

MyFormView::~MyFormView()
{
}

void MyFormView::OnDestroy()
{

	CFormView::OnDestroy();

	// TODO: Add your message handler code here
}

void MyFormView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(MyFormView, CFormView)
	ON_WM_ERASEBKGND()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_SIZE()
	ON_WM_MOUSEWHEEL()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// MyFormView diagnostics

#ifdef _DEBUG
void MyFormView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void MyFormView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// MyFormView message handlers
BOOL MyFormView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default

	//return MyFormView::OnEraseBkgnd(pDC);
	return TRUE;
}

// MyFormView message handlers
void MyFormView::SetViewSize()
{
	SetScrollSizes( MM_TEXT,CSize( CalNewZoom(m_ImageSize.cx),CalNewZoom(m_ImageSize.cy) ) );
	UpdateRulersInfo( RW_POSITION, GetScrollPosition() );
}

void MyFormView::InvalidateView()
{
	if( m_fZoomFactor < 0.05f )
	{
		m_fZoomFactor = 0.05f;
	}
	if( m_fZoomFactor > 50.0f)
	{
		m_fZoomFactor = 50.0f;
	}
	if( 0.95f < m_fZoomFactor && m_fZoomFactor < 1.05f )
	{
		m_fZoomFactor = 1.0f;
	}

	CSize sScroll;
	sScroll.cx = CalNewZoom(m_ImageSize.cx);
	sScroll.cy = CalNewZoom(m_ImageSize.cy);

	SetScrollSizes( MM_TEXT,sScroll );

	CRect rt;
	GetClientRect(&rt);

	m_tScrollPos.x = CalNewZoom(m_tOrgScrollPos.x);
	m_tScrollPos.y = CalNewZoom(m_tOrgScrollPos.y);

	int nWidth  = rt.Width();
	int nHeight = rt.Height();

	if( nWidth >= (sScroll.cx-m_tScrollPos.x) )
	{
		m_tScrollPos.x = sScroll.cx - nWidth;
		if( m_tScrollPos.x <  0 )
		{
			m_tScrollPos.x = 0;
		}

		m_tOrgScrollPos.x = CalOrgZoom(m_tScrollPos.x);
	}
	if( nHeight >= (sScroll.cy-m_tScrollPos.y) )
	{
		m_tScrollPos.y = sScroll.cy - nHeight;
		if( m_tScrollPos.y <  0 )
		{
			m_tScrollPos.y = 0;
		}
		m_tOrgScrollPos.y = CalOrgZoom(m_tScrollPos.y);
	}

	ScrollToPosition( m_tScrollPos );

	UpdateRulersInfo( RW_POSITION, m_tScrollPos );

	m_pRuler->Invalidate(TRUE);
}

void MyFormView::CalMyScrollPos()
{
	m_tScrollPos      = (POINT)GetScrollPosition();
	m_tOrgScrollPos.x = CalOrgZoom(m_tScrollPos.x);
	m_tOrgScrollPos.y = CalOrgZoom(m_tScrollPos.y);
}

void MyFormView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	CalMyScrollPos();
	UpdateRulersInfo( RW_HSCROLL, m_tScrollPos );

	CFormView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void MyFormView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	CalMyScrollPos();
	UpdateRulersInfo( RW_VSCROLL, m_tScrollPos );

	CFormView::OnVScroll(nSBCode, nPos, pScrollBar);
}

void MyFormView::UpdateRulersInfo(int nMessage, CPoint ScrollPos, CPoint Pos)
{
	stRULER_INFO pRulerInfo;
	pRulerInfo.uMessage    = nMessage;
	pRulerInfo.ScrollPos   = ScrollPos;
	pRulerInfo.Pos         = Pos;
	pRulerInfo.DocSize     = (CSize)m_ImageSize;
	pRulerInfo.fZoomFactor = m_fZoomFactor;
	pRulerInfo.fRealLenMMPerPt = m_fRealLenMMPerPt;

	m_pRuler->UpdateRulersInfo(pRulerInfo);
}


void MyFormView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	
	CFormView::OnTimer(nIDEvent);
}

BOOL MyFormView::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	switch (pMsg->message)
	{
	case WM_KEYDOWN:
		{
			switch (pMsg->wParam)
			{
			case VK_ESCAPE:
				{
					return TRUE;
				} 
				break;
			case VK_RETURN:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_RETURN,NULL);
					return TRUE;
				}
				break;
			case VK_SPACE:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_SPACE,NULL);
					return TRUE;
				}
				break;
			case VK_UP:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_UP,NULL);  
					return TRUE;
				}				
				break;
			case VK_DOWN:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_DOWN,NULL); 
					return TRUE;
				}				
				break;
			case VK_LEFT:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_LEFT,NULL); 
					return TRUE;
				}				
				break;
			case VK_RIGHT:
				{
					::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_KEYDOWN,VK_RIGHT,NULL); 
					return TRUE;
				}				
				break;
			default:
				break;
			}
		}
	default:
		break;
	}

	return CFormView::PreTranslateMessage(pMsg);
}

void MyFormView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	if( !m_bInit )
	{
		return;
	}

}

#define MouseWheelZoomRate 1.2f

void MyFormView::ZoomOut()
{
	m_fZoomFactor *= MouseWheelZoomRate;
	InvalidateView();
}

void MyFormView::ZoomIn()
{
	m_fZoomFactor /= MouseWheelZoomRate;
	InvalidateView();
}

BOOL MyFormView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: Add your message handler code here and/or call default
	if( zDelta > 0 )
	{
		ZoomIn();
	}
	else
	{
		ZoomOut();
	}

	return CFormView::OnMouseWheel(nFlags, zDelta, pt);
}

void MyFormView::InitView()
{
	m_fZoomFactor  = 1.0f;
	m_ImageSize.cx = 0;
	m_ImageSize.cy = 0;

	m_tOrgScrollPos.x = 0;
	m_tOrgScrollPos.y = 0;
	m_tScrollPos.x = 0;
	m_tScrollPos.y = 0;
}

void MyFormView::InitWnd()
{
	//m_fZoomFactor  = 1.0f;
	m_ImageSize.cx = 0;
	m_ImageSize.cy = 0;

	//m_tOrgScrollPos.x = 0;
	//m_tOrgScrollPos.y = 0;
	//m_tScrollPos.x = 0;
	//m_tScrollPos.y = 0;
}

void MyFormView::GetImageXY(CPoint &Pt)
{
	CPoint point = GetScrollPosition();
	Pt.x += point.x;
	Pt.y += point.y;

	Pt.x = CalOrgZoom(Pt.x);
	Pt.y = CalOrgZoom(Pt.y);
}

BOOL MyFormView::IsInside(int x,int y)
{
	return ( 0<=y && y<(m_ImageSize.cy) && 0<=x && x<(m_ImageSize.cx) );
}
